package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.AddressMapper;
import com.itheima.mp.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 顾清言
 * @since 2024-05-11
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    /**
     * 根据用户id查询地址列表
     * @param userId
     * @return
     */
    @Override
    public List<AddressVO> getByUserId(Long userId){
        // 1.查询用户信息
        User user = Db.lambdaQuery(User.class)
                .eq(User::getId, userId)
                .one();
        // 2.判断用户的状态
        if (user == null || user.getStatus() == UserStatus.FREEZE){
            throw new RuntimeException("用户状态异常！");
        }
        // 3.获取收货地址
        List<Address> addresses = lambdaQuery()
                .eq(Address::getUserId, userId)
                .list();
        return BeanUtil.copyToList(addresses,AddressVO.class);
    }
}
